<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm"
             :inline="true" v-show="showSearch"
             label-width="68px" style="float: left">
      <el-form-item label="模板名称" prop="assessName" >
        <el-input
          v-model="queryParams.assessName"
          placeholder="请输入评定模板名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属类别" prop="pdType" >
        <el-select v-model="queryParams.pdType" placeholder="请选择模板类别" clearable>
          <el-option
            v-for="cat in categoryList "
            :key="cat.id"
            :label="cat.cateName"
            :value="cat.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button
      type="primary"
      plain
      icon="el-icon-plus"
      size="mini"
      @click="handleAdd"
      style="float: right"
    >新增</el-button>
<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--          <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="el-icon-plus"-->
<!--            size="mini"-->
<!--            @click="handleAdd"-->
<!--            style="float: right"-->
<!--          >新增</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="assessList" @selection-change="handleSelectionChange" :default-sort = "{prop: 'assessId', order: 'descending'}">
      <el-table-column label="序号" type="index" width="100" align="center"/>
<!--      <el-table-column label="评定模板编号" align="center" prop="assessId" width="200" sortable/>-->
      <el-table-column label="模板名称" align="center" prop="assessName" />
<!--      <el-table-column :show-overflow-tooltip="true" label="评定模板配置" align="center" prop="assessConfig" width="600"/>-->
      <el-table-column label="所属类别" align="center" prop="cateName"/>
<!--      <el-table-column label="备注" align="center" prop="remark" width="100"/>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLook(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="modify(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="changeCate(scope.row)"
          >修改分类</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="400px" center>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="所属类别" prop="pdType">
          <el-select v-model="form.pdType" placeholder="请选择评定模板类别" style="width: 222px">
            <el-option
              v-for="cat in categoryList "
              :key="cat.id"
              :label="cat.cateName"
              :value="cat.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看评估表模板 -->
    <el-dialog :title="titleLook" :visible.sync="dialogVisible" width="70%" center>

      <div v-for="item in assessConfigData">
        <el-row type="flex" align="middle" style="margin-top: 10px">
          <el-col>
            <div>
              <h3 class="h2Bold">{{ item.titleName }}</h3>
            </div>
          </el-col>
        </el-row>
        <div>
          <el-row v-for="(assname,index) in item.assConfig" :key="index" :assname="assname">
            <el-col :span="22" :offset="1">
              <keep-alive>
                <component
                  :is="assname"
                  :pgForm="{}"
                ></component>
              </keep-alive>
            </el-col>
          </el-row>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listAssess, getAssess, delAssess, addAssess, updateAssess } from "@/api/gaoxin/assess";
import {listCategory} from "@/api/system/category";

export default {
  name: "Assess",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // assess表格数据
      assessList: [],
      categoryList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看按钮弹出层
      dialogVisible: false,
      // 查看按钮弹出层标题
      titleLook: '',
      // 查看按钮弹出层评估表配置数据
      assessConfigData: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        assessName: null,
        assessConfig: null,
        pdType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getListCategory();
  },
  methods: {
    /** 查询assess列表 */
    getList() {
      this.loading = true;
      listAssess(this.queryParams).then(response => {
        this.assessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListCategory() {
      listCategory().then(response => {
        this.categoryList = response.rows;
        // console.log("this.categoryList",this.categoryList)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        assessId: null,
        assessName: null,
        assessConfig: null,
        remark: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assessId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/setting/template/assess/add");
    },
    /** 查看按钮操作 */
    handleLook(row) {
      this.titleLook = row.assessName;
      // console.log('assessConfig:',row.assessConfig)
      this.assessConfigData = JSON.parse(row.assessConfig);
      this.dialogVisible = true;
    },
    /** 给修改页面传assessId */
    modify(row) {
      let assessId = row.assessId;
      this.$router.push("/setting/template/assess/update/" + assessId);
    },
    /** 修改分类按钮操作 */
    changeCate(row) {
      this.open = true;
      this.title = '修改分类';
      this.form = row;
      // console.log("row.pdType",row.pdType);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assessId != null) {
            updateAssess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assessIds = row.assessId || this.ids;
      const assessName = row.assessName;
      this.$modal.confirm(`是否删除评定模板：${assessName} ？`).then(function() {
        return delAssess(assessIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/assess/export', {
        ...this.queryParams
      }, `assess_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.h2Bold {
  font-weight: 600;
  margin: 30px 0 0 0;
}
</style>
